package com.jianyun.jyzs.dao;

import android.content.Context;
import android.text.TextUtils;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.jianyun.jyzs.bean.QualityRespon;
import com.jianyun.jyzs.db.DatabaseHelper;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.rongcloud.rce.lib.CacheTask;
import com.jianyun.jyzs.utils.RceLog;

/**
 * Created by mlli on 2017/9/20.
 */

public class QualityDao {
    private Context context;
    private Dao<QualityRespon.QualityRes,Integer> dao;
    private DatabaseHelper helper;


    public QualityDao(Context context) {
        this.context = context;
        try {
            helper = DatabaseHelper.getHelper(context);
            dao = helper.getDao(QualityRespon.QualityRes.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void insert(QualityRespon.QualityRes res){
        try {
            RceLog.d("lml","质量检查单插入 创建人"+res.getCreateUserName()+"责任人"+res.getResponUser()+"状态"+res.getWorkState());
            dao.createOrUpdate(res);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public List<QualityRespon.QualityRes> getQualityCreateByEn(String userId,String enterpriseCode){
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        try {
            QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().eq("createUserId",userId).and().eq("enterpriseCode",enterpriseCode).and().ne("workState",1);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }
    public QualityRespon.QualityRes getQualityById(String enterpriseCode,String orderid){
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        try {
            String userId = CacheTask.getInstance().getUserId();
            QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder();
            builder.where().eq("enterpriseCode",enterpriseCode).and().eq("id",orderid);
            list= builder.query();
            if (list.size()>0)return list.get(0);
            return null;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<QualityRespon.QualityRes> searchQuality(int current,String enterpriseCode, String title, String create, String response, String date) {
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        try {
            QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder().orderBy("id",false);
            Where<QualityRespon.QualityRes, Integer> where = builder.where();
            where.eq("enterpriseCode",enterpriseCode);
            where.and().ne("workState","1");
            if (!TextUtils.isEmpty(title))where.and().like("titleName","%"+title+"%");
            if (!TextUtils.isEmpty(create))where.and().like("createUserName","%"+create+"%");
            if (!TextUtils.isEmpty(response))where.and().like("responUser","%"+response+"%");
            if (!TextUtils.isEmpty(date))where.and().like("createDate","%"+date+"%");
            String userId = CacheTask.getInstance().getUserId();
            switch (current){
                case 0:
                    where.and().eq("createUserId",userId);
                    break;
                case 1:
                    where.and().eq("responUserId",userId);
//
                    break;
                case 2:
                    where.and().eq("wotype",3);
                    where.and().eq("localUserId",userId);
                    break;
            }
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    public void del(String enterpriseCode, String s) {
        try {
            DeleteBuilder<QualityRespon.QualityRes, Integer> deleteBuilder = dao.deleteBuilder();
            deleteBuilder.where().eq("enterpriseCode",enterpriseCode).and().eq("id",s);
            deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public List<QualityRespon.QualityRes> getCalendarCrm(String userId, String enterpriseCode, String date) {
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        try {
            QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().eq("enterpriseCode",enterpriseCode).and().eq("responUserId",userId).and().eq("createDate",date).and().ne("workState",1);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    public List<QualityRespon.QualityRes> getQualityResponByEn(String userId, String enterpriseCode) {
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        try {
            QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().eq("responUserId",userId).and().eq("enterpriseCode",enterpriseCode).and().ne("workState",1);
            list= builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
            return list;
        }
    }

    //参与
    public List<QualityRespon.QualityRes> getQualityPratakrByEn(String userId, String enterpriseCode) {
        RceLog.d("lml","userId="+userId+"enterpriseCode="+enterpriseCode);
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        try {
            QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder().orderBy("id",false);
            builder.where().ne("workState",1).and().eq("wotype",3).and().eq("enterpriseCode",enterpriseCode).and().eq("localUserId",userId);
            list = builder.query();
            return list;
        } catch (SQLException e) {
            e.printStackTrace();

        }
        return list;

    }

    public List<QualityRespon.QualityRes> getAll(){
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder();
        try {
            list = builder.query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public QualityRespon.QualityRes getQualityByDisId(String disId) {
        List<QualityRespon.QualityRes> list = new ArrayList<>();
        QueryBuilder<QualityRespon.QualityRes, Integer> builder = dao.queryBuilder();
        try {
            list = builder.where().eq("chatid", disId).query();
            if (list.size()>0){
                return list.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
